package com.ebelter.nb.model.http;


import android.text.TextUtils;

import com.ebelter.btcomlib.utils.GsonUtils;
import com.ebelter.nb.model.http.response.NbBaseResponse;
import com.ebelter.nb.model.http.response.BaseResponse1;
import com.ebelter.nb.ui.activitys.NBBaseActivity;
import com.lzy.okgo.callback.StringCallback;
import okhttp3.Call;
import okhttp3.Response;



public class MyStringCallback2<T extends NbBaseResponse> extends StringCallback {

    private static final String R_S_R_S="请求成功 解析成功";
    private static final String R_S_R_F="请求成功 解析失败";
    private static final String R_F = "请求失败";

    public static final int FAILURE = 0;
    public static final int SUCCESS = 1;


    private Class<T> classOfT;
    private HttpResponse2<T> httpResponse;



    public MyStringCallback2(HttpResponse2<T> httpResponse, Class<T> classOfT) {
        this.classOfT = classOfT;
        this.httpResponse = httpResponse;
    }

    @Override
    public void onSuccess(String s, Call call, Response response) {
        //先单独解析 resultCode是多少？
        BaseResponse1 baseResponse1 = GsonUtils.getInstance().fromJson(s, BaseResponse1.class);
        if (baseResponse1==null){
            //说明请求成功解析失败
            httpResponse.result(true,R_S_R_F,FAILURE,null,s);
            return;
        }
        if (NbBaseResponse.class==classOfT){//这边把结果码带回去就可以了
            NbBaseResponse nbBaseResponse=new NbBaseResponse();
            nbBaseResponse.resultCode = baseResponse1.resultCode;
            httpResponse.result(true,nbBaseResponse.message,baseResponse1.resultCode, null,s);
            return;
        }
        if (baseResponse1.resultCode == SUCCESS){//SUCCESS的值是根据协议来定的
            //说明如愿请求成功解析成功 用理想的对象去解析
            T t= GsonUtils.getInstance().fromJson(s,classOfT);
            if (t!=null){
                t.message = R_S_R_S;
                httpResponse.result(true,R_S_R_S,SUCCESS,t,s);
            }else {
                //说明请求成功解析失败
                httpResponse.result(true,R_S_R_F,FAILURE,null,s);
            }
        }else {
            httpResponse.result(true,R_S_R_S,baseResponse1.resultCode,null,s);
        }

    }

    @Override
    public void onError(Call call, Response response, Exception e) {
        super.onError(call, response, e);
        httpResponse.result(false,R_F,FAILURE,null,response!=null?response.code()+"":"");

    }
}
